//
//  SLLogger.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/17.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLLogger.h"

#import <UIKit/UIKit.h>

#import "SLDebuger.h"

@interface SLLogger ()

@property (nonatomic, copy) NSString *logString;

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIScrollView *logBackView;
@property (nonatomic, strong) UILabel *logLabel;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIButton *clearButton;

@end

@implementation SLLogger

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static SLLogger *manager;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupLogger];
    }
    return self;
}

- (void)setupLogger {
    self.logString = @"";
    
    self.backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    
    CGFloat closeButtonMarginBottom = 20.0;
    CGFloat closeButtonHeight = 36.0;
    CGFloat closeButtonMarginTopToLogBackView = 20.0;
    
    CGFloat logBackViewMarginHorizontal = 20.0;
    CGFloat logBackViewWidth = [UIScreen mainScreen].bounds.size.width - logBackViewMarginHorizontal * 2.0;
    CGFloat logBackViewMarginTop = 20.0;
    CGFloat logBackViewHeight = [UIScreen mainScreen].bounds.size.height - logBackViewMarginTop - closeButtonMarginBottom - closeButtonHeight - closeButtonMarginTopToLogBackView;
    CGRect logBackViewFrame = CGRectMake(logBackViewMarginHorizontal, logBackViewMarginTop, logBackViewWidth, logBackViewHeight);
    self.logBackView = [[UIScrollView alloc] initWithFrame:logBackViewFrame];
    [self.backView addSubview:self.logBackView];
    self.logBackView.backgroundColor = [UIColor whiteColor];
    
    CGFloat logLabelMargin = 8.0;
    CGFloat logLabelWidth = logBackViewWidth - logLabelMargin * 2.0;
    CGFloat logLabelHeight = logBackViewHeight - logLabelMargin * 2.0;
    CGRect logLabelFrame = CGRectMake(logLabelMargin, logLabelMargin, logLabelWidth, logLabelHeight);
    self.logLabel = [[UILabel alloc] initWithFrame:logLabelFrame];
    [self.logBackView addSubview:self.logLabel];
    self.logLabel.font = [UIFont systemFontOfSize:11];
    self.logLabel.numberOfLines = 0;
    self.logLabel.preferredMaxLayoutWidth = logLabelWidth;
    
    CGFloat closeButtonMarginTop = [UIScreen mainScreen].bounds.size.height - closeButtonMarginBottom - closeButtonHeight;
    CGFloat closeButtonWidth = 60.0;
    CGFloat clearButtonWidth = closeButtonWidth;
    CGFloat clearButtonMarginTop = closeButtonMarginTop;
    CGFloat clearButtonMarginLeftToCloseButton = 20.0;
    CGFloat closeButtonMarginLeft = ([UIScreen mainScreen].bounds.size.width - closeButtonWidth - clearButtonWidth - clearButtonMarginLeftToCloseButton) / 2.0;
    CGRect closeButtonFrame = CGRectMake(closeButtonMarginLeft, closeButtonMarginTop, closeButtonWidth, closeButtonHeight);
    self.closeButton = [self createButtonWithFrame:closeButtonFrame title:@"关闭" color:[UIColor whiteColor] action:@selector(closeButtonClick:)];
    [self.backView addSubview:self.closeButton];
    
    CGFloat clearButtonHeight = closeButtonHeight;
    CGFloat clearButtonMarginLeft = closeButtonMarginLeft + closeButtonWidth + clearButtonMarginLeftToCloseButton;
    CGRect clearButtonFrame = CGRectMake(clearButtonMarginLeft, clearButtonMarginTop, closeButtonWidth, clearButtonHeight);
    self.clearButton = [self createButtonWithFrame:clearButtonFrame title:@"清除" color:[UIColor whiteColor] action:@selector(clearButtonClick:)];
    [self.backView addSubview:self.clearButton];
    
}

- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title color:(UIColor *)color action:(SEL)action {
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.layer.borderWidth = 1.0;
    button.layer.borderColor = color.CGColor;
    button.layer.cornerRadius = 4.0;
    button.layer.masksToBounds = YES;
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (void)closeButtonClick:(UIButton *)sender {
    [self hideLogInfo];
}

- (void)clearButtonClick:(UIButton *)sender {
    [self hideLogInfo];
    self.logString = @"";
}

- (void)addSeparateLine {
    self.logString = [NSString stringWithFormat:@"%@\n\n\n", self.logString];
}

- (void)addLog:(NSString *)string {
    self.logString = [NSString stringWithFormat:@"%@\n%@", self.logString, string];
}

- (NSString *)getLog {
    return self.logString;
}

- (void)showLogInfo {
    [self resetLogView];
    
    self.backView.alpha = 0.0;
    [[UIApplication sharedApplication].keyWindow addSubview:self.backView];
    [UIView animateWithDuration:0.5 animations:^{
        self.backView.alpha = 1.0;
    }];
}

- (void)hideLogInfo {
    return;
    if (self.hideLogViewAction) {
        self.hideLogViewAction();
    } else {
//        [[SLDebuger sharedInstance] hideMenuView:self];
//        return;
        [UIView animateWithDuration:0.5 animations:^{
            self.backView.alpha = 0;
        } completion:^(BOOL finished) {
            if (finished && self.backView.superview) {
                [self.backView removeFromSuperview];
            }
        }];
    }
}

- (UIView *)resetLogView {
    self.logLabel.text = [self getLog];
    [self.logLabel sizeToFit];
    self.logBackView.contentSize = CGSizeMake(0, self.logLabel.frame.size.height + 20.0);
    
    if (self.backView.superview) {
        [self.backView removeFromSuperview];
    }
    
    return self.backView;
}

@end
